<?php
class AppNoteAction extends Action {
	/**
	 * 帖子列表
	 * 入参,排序字段,用户id
	 * get
	 **/
	public function noteList(){
		/*if (!$this->isGet()){
		 $result['code']='0';
		 $result['msg']="请求错误";
		 $this->ajaxReturn($result,'JSON');
		 }*/
		$note = M('Note');
		$str = $_GET['str'];
		if(!empty($str)){
			$where['s_note.type'] = $str;
		}
		if(empty($_GET['id'])){
			$noteList = $note ->field('s_note.*,u.username,ifnull(note.note_count,0) as note_count,(TO_DAYS(now()) - TO_DAYS(s_note.create_time)) as days') ->join("left join s_user u on u.id=s_note.user_id
        	left join (select count(id) as note_count,note_id from s_note_one group by note_id) as note on note.note_id= s_note.id")-> order('s_note.top desc,s_note.create_time desc')->where($where) -> select();
		}else{
			$where['s_note.user_id'] = $_GET['id'];//我的帖子
			$noteList = $note ->field('s_note.*,u.username,ifnull(note.note_count,0) as note_count,(TO_DAYS(now()) - TO_DAYS(s_note.create_time)) as days') ->join("left join s_user u on u.id=s_note.user_id
        	left join (select count(id) as note_count,note_id from s_note_one group by note_id) as note on note.note_id= s_note.id")->where($where)-> order('s_note.top desc,s_note.create_time desc') -> select();
		}

		//        echo var_dump($courseList);
		//        $this->display();
		$result['noteList'] = $noteList;
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 帖子详细
	 * 入参,课程id
	 * get
	 */
	public function noteDitail(){
		$note = M('Note');
		$where['id'] = $_GET['id'];//$_GET['id']
		$count=$note->where($where)->getField('number');
		//查看列表查看次数加一
		$counts = (int)$count + 1;
		$data['number'] = $counts;
		$note->where($where)->data($data)->save();
		//课程
		$note = M('Note');
		$where_id['s_note.id'] = $_GET['id'];
		$noteList = $note ->field('s_note.*,u.username,ifnull(note.note_count,0) as note_count,u.image as image_user,(TO_DAYS(now()) - TO_DAYS(s_note.create_time)) as days') ->join("left join s_user u on u.id=s_note.user_id
        	left join (select count(id) as note_count,note_id from s_note_one group by note_id) as note on note.note_id= s_note.id")->where($where_id)->find();
		//评论
		$note_one = M('Note_one');
		$note_id['note_id'] = $_GET['id'];
		$noteOneList = $note_one->field('s_note_one.*,u.username,u.image as image_user,(TO_DAYS(now()) - TO_DAYS(s_note_one.create_time)) as days')->join("left JOIN s_user u on s_note_one.user_id = u.id ")->where($note_id)->select();
		//二级评论
		$note_two = M('Note_two');
		$note_id_two["o.note_id"] = $_GET['id'];
		$noteTwoList = $note_two->field('s_note_two.*,u.username as name,u.image as image_user,uu.username as twoname,(TO_DAYS(now()) - TO_DAYS(s_note_two.create_time)) as days')->where($note_id_two)->join("left JOIN s_user u on s_note_two.user_id = u.id left JOIN s_note_one o on s_note_two.note_one_id = o.id left JOIN s_user uu on o.user_id = uu.id  ")->select();
		//图片
		$note_image = M('Note_image');
		$note_id_image["note_id"] = $_GET['id'];
		$noteImage = $note_image->where($note_id_image)->select();
		//一级图片
		$note_image = M('Note_one_image');
		$note_one_id_image["note_one_id"] = $_GET['id'];
		$noteOneImage = $note_image->where($note_one_id_image)->select();
		//二级图片
		$note_image = M('Note_image');
		$note_id_image["note_id"] = $_GET['id'];
		$noteImage = $note_image->where($note_id_image)->select();
		 
		$result['noteList'] = $noteList;//列表
		if(!empty($noteImage)){
			$result['noteImage'] = $noteImage;
		}else{
			$result['noteImage'] = null;
		}
		$result['noteOneList'] = $noteOneList;//列表评论
		$result['noteTwoList'] = $noteTwoList;//二级评论
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 新增帖子
	 * 入参
	 * post
	 */
	public function addNote(){
		$note = M('Note');
		$note->create();
		$notes['title']=$_POST['title'];
		$notes['user_id']=$_POST['user_id'];
		$notes['content']=$_POST['content'];
		$notes['type']=$_POST['type'];
		$notes['create_time']=date('Y-m-d H:i',time());
		$suc = $note->data($notes)->add();
		//base64Data
		$base64=$_POST['base64Data'];
		$sss = '';
		if(empty($base64) || $base64 == null || $base64 == ""){
			$notess['image']='';
		}else{
			$arr_str=explode("---",$base64);//以,为拆分条件
			for($i=1;$i<count($arr_str);$i++){
				$s = base64_decode($arr_str[$i]);
				$uniqid = 'note'.time();
				$image = file_put_contents('./Public/Uploads/image/note/'.$uniqid.$i.'.png', $s);
				$sss = $sss.'-'.$uniqid.$i.'.png';
				//页面用
				if($i>0){
					$notes_image['note_id'] = $suc;
					$notes_image['image'] = $uniqid.$i.'.png';
					$note_image = M('Note_image');
					$note_image->create();
					$note_image->data($notes_image)->add();
				}
			}
			$notess['image'] = substr($sss,1);
//				$notes_image['note_id'] = $suc;
//				$note_image = M('Note_image');
//				$note_image->create();
//				$note_image->data($notes_image)->add();
			/*$s = base64_decode($arr_str[1]);
	//		$s = base64_decode(str_replace('data:image/jpeg;base64,', '', $base64));
			$uniqid = 'note'.time();
			$image = file_put_contents('./Public/Uploads/image/note/'.$uniqid.'.png', $s);
			$notes['image'] = $uniqid.'.png';*/
		}
		$where['id'] = $suc;
		if($notess['image'] == false){
			$notess['image'] = '';
		}
		$note->data($notess)->where($where)->save();
		
		$result['suc'] = $suc;
		$result['type'] = $_POST['type'];
		$result['base64'] = $base64;
		$this->ajaxReturn($result,'JSON');
	}
	/**
	 * 回复楼主
	 * 入参user_id(session),note_id,message
	 * post
	 */
	public function noteOneAdd(){
		 
		$note_one = M('Note_one');
		$note_one->create();
		$noteOne['note_id']=$_POST['note_id'];
		$noteOne['user_id']=$_POST['user_id'];
		$noteOne['message']=$_POST['message'];
		$noteOne['create_time']=date('Y-m-d H:i',time());
		$suc = $note_one->add($noteOne);
		$sss = '';
		//base64Data
		$base64=$_POST['base64Data'];
		if(empty($base64) || $base64 == null || $base64 == ""){
			$noteOnes['image']='';
		}else{
			$arr_str=explode("---",$base64);//以,为拆分条件
			for($i=1;$i<count($arr_str);$i++){
				$s = base64_decode($arr_str[$i]);
				$uniqid = 'note'.time();
				$image = file_put_contents('./Public/Uploads/image/noteOne/'.$uniqid.$i.'.png', $s);
				$sss = $sss.'-'.$uniqid.$i.'.png';
				//页面用
				if($i>0){
					$notes_image['note_one_id'] = $suc;
					$notes_image['note_id'] = $_POST['note_id'];
					$notes_image['image'] = $uniqid.$i.'.png';
					$note_image = M('Note_one_image');
					$note_image->create();
					$note_image->data($notes_image)->add();
				}
			}
			$noteOnes['image'] = substr($sss,1);
			/*$arr_str=explode(",",$base64);//以,为拆分条件
			$s = base64_decode($arr_str[1]);
	//		$s = base64_decode(str_replace('data:image/jpeg;base64,', '', $base64));
			$uniqid = 'note'.time();
			$image = file_put_contents('./Public/Uploads/image/noteOne/'.$uniqid.'.png', $s);
			$noteOne['image'] = $uniqid.'.png';*/
		}
		$where['id'] = $suc;
		if($noteOnes['image'] == false){
			$noteOnes['image'] = '';
		}
		$note_one->data($noteOnes)->where($where)->save();
		$this->ajaxReturn($base64,'JSON');
	}
	/**
	 * 回复评论
	 * 入参user_id(session),note_id,note_one_id,words
	 * post
	 */
	public function noteTwoAdd(){
		 
		$note_two = M('Note_two');
		$note_two->create();
		$noteTwo['note_id']=$_POST['note_id'];
		$noteTwo['note_one_id']=$_POST['note_one_id'];
		$noteTwo['user_id']=$_POST['user_id'];
		$noteTwo['words']=$_POST['words'];
		$noteTwo['create_time']=date('Y-m-d H:i',time());
		$suc = $note_two->add($noteTwo);
		$sss = '';
		//base64Data
		$base64=$_POST['base64Data'];
		if(empty($base64) || $base64 == null || $base64 == ""){
			$noteTwos['image']='';
		}else{
			$arr_str=explode("---",$base64);//以,为拆分条件
			for($i=1;$i<count($arr_str);$i++){
				$s = base64_decode($arr_str[$i]);
				$uniqid = 'note'.time();
				$image = file_put_contents('./Public/Uploads/image/noteTwo/'.$uniqid.$i.'.png', $s);
				$sss = $sss.'-'.$uniqid.$i.'.png';
				//页面用
				if($i>0){
					$notes_image['note_two_id'] = $suc;
					$notes_image['note_id'] = $_POST['note_id'];
					$notes_image['image'] = $uniqid.$i.'.png';
					$note_image = M('Note_two_image');
					$note_image->create();
					$note_image->data($notes_image)->add();
				}
			}
			$noteTwos['image'] = substr($sss,1);
			/*$arr_str=explode(",",$base64);//以,为拆分条件
			$s = base64_decode($arr_str[1]);
	//		$s = base64_decode(str_replace('data:image/jpeg;base64,', '', $base64));
			$uniqid = 'note'.time();
			$image = file_put_contents('./Public/Uploads/image/noteTwo/'.$uniqid.'.png', $s);
			$noteTwo['image'] = $uniqid.'.png';*/
		}
		$where['id'] = $suc;
		if($noteTwos['image'] == false){
			$noteTwos['image'] = '';
		}
		$note_two->data($noteTwos)->where($where)->save();
		
		$this->ajaxReturn($base64,'JSON');
	}
}
?>